package com.example.xm.controller;

import cn.hutool.core.date.DateUtil;
import com.example.xm.Entity.Commodity;
import com.example.xm.Entity.Mail;
import com.example.xm.Entity.Pl;
import com.example.xm.config.PageResult;
import com.example.xm.servive.PlServive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/P")
public class PlController {
    @Autowired
    PlServive plServive;

    /**显示*/
    @PostMapping("/spper")
    public List<Pl> Students(@RequestBody Pl pl){
        System.out.println(plServive.getAllPl(pl));
        return plServive.getAllPl(pl);
    }

    /**分页*/
    @GetMapping("/pager")
    public PageResult selectPager(@RequestParam(name = "pageNum",required = false,defaultValue = "1") int pageNum,
                                  @RequestParam(name = "pageSize",required = false,defaultValue = "2") int pageSize,
                                  @RequestParam(required = false,defaultValue = "")String pl_id,
                                  @RequestParam(required = false,defaultValue = "")String pl_plnr,
                                  @RequestParam(required = false,defaultValue = "")String pl_date,
                                  @RequestParam(required = false,defaultValue = "")String pl_qq,
                                  @RequestParam(required = false,defaultValue = "")String pl_sp_id,
                                  @RequestParam(required = false,defaultValue = "")String name,
                                  @RequestParam(required = false,defaultValue = "")String img1,
                                  @RequestParam(required = false,defaultValue = "")String mail_img
    ){
        //获取所有评论信息
        Pl p = new Pl();
        p.setPl_id(pl_id);
        p.setPl_plnr(pl_plnr);
        p.setPl_qq(pl_qq);
        p.setPl_sp_id(pl_sp_id);
        if(!pl_date.equals("") && !pl_date.equals("null")){
            p.setPl_date(DateUtil.parse(pl_date,"yyyy-MM-dd"));
        }
        p.setCommodity(new Commodity());
        p.getCommodity().setName(name);
        p.getCommodity().setImg1(img1);
        p.setMail(new Mail());
        p.getMail().setMail_img(mail_img);
        return plServive.selectPager(pageNum,pageSize,p);
    }

    /**添加评论*/
    @PostMapping("")
    public int AddPl(@RequestBody Pl pl){
        pl.setPl_date(new Date());
        System.out.println("添加了：" + pl);
        return plServive.AddPl(pl);
    }
}
